Skip to content

fix(relinker): stop running a guest module's preinit array from the startup stubs - #579

Merged
RINNECODER merged 1 commit into
mainfrom
fix/libc-double-init
Oct 19, 2026
Merged

RINNECODER merged 1 commit into
mainfrom
fix/libc-double-init

Conversation

@RINNECODER

Copy link
Copy Markdown
Owner

What

A converted guest module's initializer is the console CRT entry, which walks DT_PREINIT_ARRAY itself. The macOS, Linux and Windows startup stubs also ran the array before that initializer, so every preinit entry ran twice. A module that registers a thread destructor callback from a preinit entry aborted on the second registration (ThreadLifecycle::SetThreadDtors throws Thread destructor callback is already registered), inside dyld initializers at 0.5 s of guest time.

This reverts the startup-stub execution of the preinit array and drops the reader's copy of the array, matching the upstream revert of boykopovar#6112. The unused-NID filter still collects the preinit targets as roots, so the entries stay in the image.

guest_preinit_once covers the contract. It builds a module whose DT_INIT walks the preinit array (the CRT pattern), relinks it, and asserts no macOS startup stub calls the preinit slots. It also asserts a single run of each entry where native execution is available. It fails against the published engine's relinker (both preinit slots present in the macOS stub) and passes after the change.

Tested

macOS 26.x, Apple Silicon, relinker-only build with BUILD_TESTING=ON:

  • guest_preinit_once: fails against the pre-fix relinker (macOS stub calls both preinit slots), passes after the change.
  • check_conventions.py --base origin/main: all checks pass.
  • Rosetta engine build boots the title under the GPU lock: guest entry at 0.615 s, 2745 frames presented in a 140 s run, main menu <= 69 s, leader select <= 81 s, map generation start <= 102 s, exit 0, no fault, no uncaught exception. The pre-fix engine aborted at 0.505 s with exit 134.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: title bring-up that hits the double preinit run
  • GPUs tested: n/a (relinker only)
  • AI-assisted: yes

…tartup stubs

A converted guest module's initializer is the console CRT entry, which walks
DT_PREINIT_ARRAY itself. The macOS, Linux and Windows startup stubs also ran
the array before that initializer, so every preinit entry ran twice. A module
that registers a thread destructor callback from a preinit entry aborted on
the second registration.

This reverts the startup-stub execution added for the preinit array and drops
the reader's copy of the array, matching the upstream revert. The unused-NID
filter still collects the preinit targets as roots.

guest_preinit_once covers the contract: it fails while a macOS startup stub
still calls the preinit slots, and asserts a single run of each entry where
native execution is available.
@RINNECODER

Copy link
Copy Markdown
Owner Author

@codex review

@coderabbitai

coderabbitai Bot commented Oct 19, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 23b55926-a278-4b62-a90b-3861b14b0009
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T07:27:40.360853Z fa263ed Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fa263edf49

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

}
};
readLifecycleArray(32, 33, image.PreInitArray);
readLifecycleArray(25, 27, image.InitArray);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep validating the preinit array before discarding it

When a guest has a non-empty DT_PREINIT_ARRAY with a malformed size/address or a relocation targeting mapped but non-executable data, the generic relocation checks can accept it; this removed call was the only path through readLifecycleArray that enforced the array layout, mapping, and executable function targets. Because initialization now relies on the guest's DT_INIT routine to invoke these entries, conversion succeeds but can fault during initialization instead of rejecting the invalid module. Keep this validation even if the collected slot RVAs are no longer stored.

Useful? React with 👍 / 👎.

@RINNECODER
RINNECODER merged commit a6ef3c2 into main Oct 19, 2026
1 check passed
@RINNECODER
RINNECODER deleted the fix/libc-double-init branch October 19, 2026 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant